
//日期工具

/**
 * 解析时间
 * @param {(Object|string|number)} time
 * @param {string} pattern
 * @returns {string | null}
 */
export function parseTime(time, pattern) {
    if (arguments.length === 0 || !time) {
        console.warn("未传入正确的时间格式：new Date()!");
        return null;
    }
    const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
    let date;
    if (typeof time === "object") {
        date = time;

    } else {
        if ((typeof time === "string") && (/^[0-9]+$/.test(time))) {
            time = parseInt(time);
        } else if (typeof time === "string") {
            time = time.replace(new RegExp(/-/gm), "/");

        }
        if ((typeof time === "number") && (time.toString().length === 10)) {
            time = time * 1000;
        }
        date = new Date(time);
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    };
    return format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key];
        // Note: getDay() returns 0 on Sunday
        if (key === "a") {
            return ["日", "一", "二", "三", "四", "五", "六"][value];
        }
        if (result.length > 0 && value < 10) {
            value = "0" + value;
        }
        return value || 0;
    });
}

/**
 * 时间格式化
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
    if (('' + time).length === 10) {
        time = parseInt(time) * 1000
    } else {
        time = +time
    }
    const d = new Date(time)
    const now = Date.now()

    const diff = (now - d) / 1000

    if (diff < 30) {
        return '刚刚'
    } else if (diff < 3600) {
        // less 1 hour
        return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
        return '1天前'
    }
    if (option) {
        return parseTime(time, option)
    } else {
        return (
            d.getMonth() +
            1 +
            '月' +
            d.getDate() +
            '日' +
            d.getHours() +
            '时' +
            d.getMinutes() +
            '分'
        )
    }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
    const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
    if (!search) {
        return {}
    }
    const obj = {}
    const searchArr = search.split('&')
    searchArr.forEach(v => {
        const index = v.indexOf('=')
        if (index !== -1) {
            const name = v.substring(0, index)
            const val = v.substring(index + 1, v.length)
            obj[name] = val
        }
    })
    return obj
}

export function findSpecificWeekdays(startDate, endDate, dayOfWeek,range_time) {
    // 创建一个对象存储结果
    let result = {};
    // 将输入的字符串转换为Date对象
    let currentDate = new Date(startDate);
    let end = new Date(endDate);
    
    // 确保开始日期不大于结束日期
    if (currentDate > end) {
        throw new Error('开始日期不能大于结束日期');
    }
    
    // 调整开始日期到最近的一个指定的星期几
    while (currentDate.getDay() !== dayOfWeek) {
        currentDate.setDate(currentDate.getDate() + 1);
    }
    // 循环直到当前日期超过结束日期
    while (currentDate <= end) {
        const dateStr = currentDate.toISOString().split('T')[0]; // 获取 YYYY-MM-DD 格式
        result[dateStr] = range_time; // 设置时间段
        // 移动到下一个相同的星期几
        currentDate.setDate(currentDate.getDate() + 7);
    }
    return result;
}
